package com.cn.jx.common.controller;

import java.text.MessageFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import com.cn.jx.manager.po.Resource;
import com.cn.jx.manager.service.query.IDictServiceQry;
import com.cn.jx.manager.service.query.IResourceServiceQry;
import com.cn.jx.manager.service.query.ISystemInfoServiceQry;
import com.jx.base.SessionUser;
import com.jx.enumType.DataGrideTypeEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.model.ButtonElement;
import com.jx.util.Constant;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月2日
 * <p>功能描述：基本的BaseController控制层 
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
public abstract class BaseController {

	@Autowired
	protected IResourceServiceQry resourceServiceQry;

	@Autowired
	protected ISystemInfoServiceQry systemInfoServiceQry;

	protected static Logger log = LoggerFactory.getLogger("exception");
	
	protected SessionUser getSessionUser(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute(Constant.SESSION_ADMIN_KEY);
		if (obj != null) {
			SessionUser sessUser = (SessionUser) obj;
			return sessUser;
		}
		return null;
	}

	protected void setModelColumns(Model model, DataGrideTypeEnum gridType) {
		String key = MessageFormat.format("{0}columns", gridType.getId());
		model.addAttribute(key, systemInfoServiceQry.findDataGrideByType(gridType));
	}

	protected void setModelPermit(Model model, ResourceEnum parentcode,List<String> resource) {
		//得到资源
		List<Resource> listRes=	resourceServiceQry.findAllResourceByParentCode(parentcode.getCode());
		if(listRes!=null && listRes.size()>0) {
			for(Resource res:listRes) {
				ButtonElement btnEle=new ButtonElement();
				btnEle.setText(res.getText());
				btnEle.setIcons(res.getIconCls());
				btnEle.setPermit(resource.contains(res.getCode())?true:false);
				btnEle.setUrl(res.getUrl());
				btnEle.setId(res.getId());
				model.addAttribute(res.getCode(), btnEle);
			}
		}
	}
}
